#!/usr/bin/env ruby -w
#--
# Project:      pkgman
# File:         test/test_pkgman.rb
# Revision:     $Id: test_pkgman.rb 310 2008-01-04 17:12:30Z someword $
# Home:         http://code.google.com/p/pkgman/
# Author:       Derek Olsen <someword at gmail dot com>
# Copyright:    (c) 2007 by Derek Olsen
# License:      GNU General Public License v2 
#++


# Prepend the application directory to the load path
$:.unshift File.join(File.dirname(__FILE__), "..", "bin")

# Load up required supporting files
require 'pkgman'
require 'test/unit'

# Class to hold the various test cases
class PkgManTest < Test::Unit::TestCase


  def installed_version(n)
    %x{pkgparam #{n} VERSION}.chomp.to_s
  end
 
  def installed(n)
    system "pkginfo -q #{n}"
  end

  def test_package_upgrade
     assert_equal "1.1b", installed_version("ZZZpkgman-upgrade-test")
  end

  def test_multiple_install_and_remove
    assert installed("ZZZpkgman-install-a0")
    assert installed("ZZZpkgman-install-a1")
    assert installed("ZZZpkgman-install-a2")
    assert installed("ZZZpkgman-install-a3")
    assert installed("ZZZpkgman-install-a4")
    assert installed("ZZZpkgman-install-a5")
    assert installed("ZZZpkgman-install-a6")
    assert installed("ZZZpkgman-install-a7")
    assert installed("ZZZpkgman-install-a8")
    assert installed("ZZZpkgman-install-a9")
    assert installed("ZZZpkgman-install-b0")
    assert installed("ZZZpkgman-install-b1")
    assert installed("ZZZpkgman-install-b2")
    assert installed("ZZZpkgman-install-b3")
    assert installed("ZZZpkgman-install-b4")
    assert installed("ZZZpkgman-install-b5")
    assert installed("ZZZpkgman-install-b6")
    assert installed("ZZZpkgman-install-b7")
    assert installed("ZZZpkgman-install-b8")
    assert installed("ZZZpkgman-install-b9")
    assert_equal false, installed("ZZZpkgman-remove-y0")
    assert_equal false, installed("ZZZpkgman-remove-y1")
    assert_equal false, installed("ZZZpkgman-remove-y2")
    assert_equal false, installed("ZZZpkgman-remove-y3")
    assert_equal false, installed("ZZZpkgman-remove-y4")
    assert_equal false, installed("ZZZpkgman-remove-y5")
    assert_equal false, installed("ZZZpkgman-remove-y6")
    assert_equal false, installed("ZZZpkgman-remove-y7")
    assert_equal false, installed("ZZZpkgman-remove-y8")
    assert_equal false, installed("ZZZpkgman-remove-y9")
    assert_equal false, installed("ZZZpkgman-remove-z0")
    assert_equal false, installed("ZZZpkgman-remove-z1")
    assert_equal false, installed("ZZZpkgman-remove-z2")
    assert_equal false, installed("ZZZpkgman-remove-z3")
    assert_equal false, installed("ZZZpkgman-remove-z4")
    assert_equal false, installed("ZZZpkgman-remove-z5")
    assert_equal false, installed("ZZZpkgman-remove-z6")
    assert_equal false, installed("ZZZpkgman-remove-z7")
    assert_equal false, installed("ZZZpkgman-remove-z8")
    assert_equal false, installed("ZZZpkgman-remove-z9")
  end

  def test_action_missing_key_action
    PkgMan.empty_errors
    missing_action = {:hosts=>"default", :file=>"somefile"}
    p = PkgMan.new(missing_action)
    p.check_syntax(missing_action)
    assert PkgMan.config_errors.size == 1
  end

  def test_action_remove_missing_key_name
    PkgMan.empty_errors
    missing_name = {:hosts=>"default", :action=>"remove"}
    p = PkgMan.new(missing_name)
    p.check_syntax(missing_name)
    assert PkgMan.config_errors.size == 1
  end

  def test_action_remove_missing_key_hosts
    PkgMan.empty_errors
    missing_hosts = {:name=>"SUNWzero", :action=>"remove"}
    p = PkgMan.new(missing_hosts)
    p.check_syntax(missing_hosts)
    assert PkgMan.config_errors.size == 1
  end

  def test_action_install_missing_key_hosts
    PkgMan.empty_errors
    missing_hosts = {:name=>"SUNWzero", :action=>"install", :file=>"screen-4.0.2-sol10-i86pc", :version=>"4.0.2"}
    p = PkgMan.new(missing_hosts)
    p.check_syntax(missing_hosts)
    assert PkgMan.config_errors.size == 1
  end

  def test_action_install_missing_key_name
    PkgMan.empty_errors
    missing_name = {:hosts=>"default", :action=>"install", :file=>"screen-4.0.2-sol10-i86pc", :version=>"4.0.2"}
    p = PkgMan.new(missing_name)
    p.check_syntax(missing_name)
    assert PkgMan.config_errors.size == 1
  end
  
  def test_action_install_missing_key_file
    PkgMan.empty_errors
    missing_file = {:name=>"screen", :hosts=>"default", :action=>"install", :version=>"4.0.2"}
    p = PkgMan.new(missing_file)
    p.check_syntax(missing_file)
    assert PkgMan.config_errors.size == 1
  end

  def test_gzipped_file
    gzip_file = {:name=>"ZZZpkgman-gzip-test", :hosts=>"default", :action=>"install", :version=>"1.00", :file=>"ZZZpkgman-gzip-test.gz"}
    p = PkgMan.new(gzip_file)
    PkgMan.script_prep
    p.pkg_get
    p.file_gunzip
    assert p.file == "ZZZpkgman-gzip-test"
    assert `/bin/file #{WORKDIR}/#{p.file}` =~ /.*package datastream$/
    PkgMan.script_clean
  end

  def test_settings_cmd_prefix_pfexec
    settings = {:http_host=>"webserver", :http_port=>"80", :http_uri=>"/pkgs", :logging=>"syslog", :syslog_facility=>"daemon", :syslog_level=>"notice", :cmd_prefix=>"pfexec"}

    PkgMan.create_settings(settings)
    PkgMan.set_cmd_prefix
    assert PkgMan.get_settings.values_at(:cmd_prefix).to_s == "pfexec"
  end

  def test_settings_cmd_prefix_sudo
    settings = {:http_host=>"webserver", :http_port=>"80", :http_uri=>"/pkgs", :logging=>"syslog", :syslog_facility=>"daemon", :syslog_level=>"notice", :cmd_prefix=>"sudo"}
    PkgMan.create_settings(settings)
    PkgMan.set_cmd_prefix
    assert PkgMan.get_settings.values_at(:cmd_prefix).to_s == "sudo"
  end

  def test_settings_cmd_prefix_off
    settings = {:http_host=>"webserver", :http_port=>"80", :http_uri=>"/pkgs", :logging=>"syslog", :syslog_facility=>"daemon", :syslog_level=>"notice", :cmd_prefix=>"off"}
    PkgMan.create_settings(settings)
    PkgMan.set_cmd_prefix
    assert PkgMan.get_settings.values_at(:cmd_prefix).to_s.empty? 
  end

  def test_solaris_release_pkgadd_args
    PkgMan.pkgadd_zone_support(8)
    assert PkgMan.get_settings.values_at(:pkgadd_args).to_s.empty? 
    PkgMan.pkgadd_zone_support(9)
    assert PkgMan.get_settings.values_at(:pkgadd_args).to_s.empty? 
    PkgMan.pkgadd_zone_support(10)
    assert PkgMan.get_settings.values_at(:pkgadd_args).to_s == "-G"
    PkgMan.pkgadd_zone_support(11)
    assert PkgMan.get_settings.values_at(:pkgadd_args).to_s == "-G"
  end

end

